<?php
namespace app\gym\controller;
use app\common\controller\AdminBase;
/*租用预约列表*/
class GymCabinetSubscribe extends AdminBase{
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();
    }
	function index(){
		$shop_id=sp_get_current_shop_id();
		echo $this->fetch();
	}	
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	
	/*添加*/
	function add(){
		$shop_id=sp_get_current_shop_id();
		$list=db("GymCabinet")->alias("a")->join([["m_gym_cabinet_region b","a.region_id=b.id","LEFT"]])->where("a.shop_id=$shop_id")->field("a.id,concat(b.name,'-',a.cabinet_no) as text")->select();
		$this->assign("cabinets",$list);
		echo $this->fetch();
	}
	function add_Post(){
		if(request()->isPost()){
			db("GymCabinetSubscribe")->insert([
					"shop_id"=>sp_get_current_shop_id(),
					"money"=>input("money"),
					"cabinet_id"=>input("cabinet_id"),
					"member_id"=>input("member_id"),
					"start_date"=>input("start_date"),
					"end_date"=>input("end_date"),
					"descs"=>input("descs"),
			]);
			$this->ajax_success("成功");
		}	
	}
	/**
	 * 修改
	 * */
	function edit(){
		$shop_id=sp_get_current_shop_id();
		$model=db("GymCabinetSubscribe")->find(input("id",0));
		$this->assign("model",$model);
		$this->assign("member_info",db("GymMember")->where("id=".$model['member_id'])->value("concat(name,'----',mobile)"));
		$list=db("GymCabinet")->alias("a")->join([["m_gym_cabinet_region b","a.region_id=b.id","LEFT"]])->where("a.shop_id=$shop_id")->field("a.id,concat(b.name,'-',a.cabinet_no) as text")->select();
		$this->assign("cabinets",$list);
		echo $this->fetch();
	}
	/**
	 * 
	 * */
	function edit_post(){
		if(request()->isPost()){
			db("GymCabinetSubscribe")->update([
					"id"=>input("id"),
					"money"=>input("money"),
					"cabinet_id"=>input("cabinet_id"),
					"start_date"=>input("start_date"),
					"end_date"=>input("end_date"),
					"descs"=>input("descs"),
			]);
			$this->ajax_success("成功");
		}
	}
	/**
	 * 删除
	 * */
	function delete(){
		$id=input("id");
		$shop_id=sp_get_current_shop_id();
		db("GymCabinetSubscribe")->where("shop_id=$shop_id and id=$id")->delete();
		$this->ajax_success("成功");
	} 
	/**
	 * 获取会员数据
	 * */
	function  ajax_member_data(){
        $shop_id = sp_get_current_shop_id();
        $search = input("search","");
        if (mb_strlen($search) < 2) {
            $this->ajax(array());
        }
        $page = input("page/d");
        $limit = 10;
        $where = array("m.shop_id"=>array("eq", sp_get_current_shop_id()),"m.name|m.mobile" => array("like", "%{$search}%"));
        $count = db("gym_member")->alias('m')
            ->where($where)
            ->count(1);
        $list = db("gym_member")->alias("m")
            ->where($where)
            ->page($page, $limit)
            ->field("m.id, CONCAT(m.name, '--', m.mobile) as text")
            ->select();
        $this->ajax(array("results" => $list, "more" => $count > $limit * $page));
	}
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10091003");
		$this->assign("menus",$auth_group);
	}
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
		 'member_name'=>array("field" =>"b.name","operator" =>"like"),
		 'member_mobile'=>array("field" =>"b.mobile","operator" =>"like"),
		);
		$shop_id=sp_get_current_shop_id();
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$where_and.=" and a.shop_id=$shop_id ";
		$field="a.id,a.start_date,a.end_date,a.money,b.name,b.mobile,a.create_time,a.descs,c.cabinet_no,d.name as region_name";
		$total=db("GymCabinetSubscribe")->alias("a")->join([['m_gym_member b',"a.member_id=b.id ","LEFT"],['m_gym_cabinet c',"c.id=a.cabinet_id","LEFT"],['m_gym_cabinet_region d',"d.id=c.region_id","LEFT"]])->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("GymCabinetSubscribe")->alias("a")->field($field)->join([['m_gym_member b',"a.member_id=b.id ","LEFT"],['m_gym_cabinet c',"c.id=a.cabinet_id","LEFT"],['m_gym_cabinet_region d',"d.id=c.region_id","LEFT"]])->where($where_and)->order("a.id desc")->limit($this->get_limit())->fetchsql(FALSE)->select();
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}

}
